﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

using Eflong.KJFiles;

namespace KWBridge
{
    public partial class ParasForm : Form
    {
        public KWBridgeForm mainForm;

        /// <summary>
        /// 监控系统1的传感器状态的报警选项字典
        /// </summary>
        public Dictionary<string , CheckBox> dic_StatesCheckBox = new Dictionary<string , CheckBox>( );

        /// <summary>
        /// 监控系统1的传感器类型的报警选项字典，含模拟量及开关量传感器
        /// </summary>
        public Dictionary<string , CheckBox> dic_TypesCheckBox = new Dictionary<string , CheckBox>( );

        /// <summary>
        /// 监控系统2的传感器状态的报警选项字典
        /// </summary>
        public Dictionary<string , CheckBox> dic_StatesCheckBox2 = new Dictionary<string , CheckBox>( );

        /// <summary>
        /// 监控系统2的传感器类型的报警选项字典，含模拟量及开关量传感器
        /// </summary>
        public Dictionary<string , CheckBox> dic_TypesCheckBox2 = new Dictionary<string , CheckBox>( );

        internal Eflong.Config.KWBridgeConfig cfg = Eflong.Config.KWBridgeConfig.Instance;

        public ParasForm( )
        {
            //mainForm = form;
            InitializeComponent( );
            this.Load += new EventHandler( ParasForm_Load );
        }

        private void ParasForm_Load( object sender , EventArgs e )
        {
            InitData( );
            ConvertParasFrom( );
        }

        public ParasForm1 parasForm1;

        public ParasForm2 parasForm2;

        internal virtual void InitData( )
        {
        }

        /// <summary>
        /// 把配置信息设置到当前的UI中
        /// </summary>
        internal virtual void ConvertParasFrom( )
        {
            // 设置开关量的状态显示
            foreach ( string type in cfg.KJSysParas.KJDigitalStates_dic.Keys )
            {
                if ( cfg.KJSysParas.KJDigitalStates_dic[ type ] == 1 )
                {
                    // 1 开 ； 0 停
                    dic_ComboBox[ type ].SelectedIndex = 0;
                }
                else
                {
                    dic_ComboBox[ type ].SelectedIndex = 1;
                }
            }

            // 设置模拟量的报警类型
            foreach ( string type in cfg.KJSysParas.KJAnalogAlarms_dic.Keys )
            {
                dic_TypesCheckBox[ type ].Checked = cfg.KJSysParas.KJAnalogAlarms_dic[ type ];
            }

            // 设置开关量的报警类型
            foreach ( string type in cfg.KJSysParas.KJDigitalAlarms_dic.Keys )
            {
                dic_TypesCheckBox[ type ].Checked = cfg.KJSysParas.KJDigitalAlarms_dic[ type ];
            }

            // 设置传感器报警状态
            foreach ( string type in cfg.KJSysParas.KJAlarmStates_dic.Keys )
            {
                dic_StatesCheckBox[ type ].Checked = cfg.KJSysParas.KJAlarmStates_dic[ type ];
            }
            if ( !cfg.IsSingleKJSystem )
            {
                // 设置开关量的状态显示
                foreach ( string type in cfg.KJSysParas2.KJDigitalStates_dic.Keys )
                {
                    if ( cfg.KJSysParas2.KJDigitalStates_dic[ type ] == 0 )
                    {
                        dic_ComboBox2[ type ].SelectedIndex = 0;
                    }
                    else
                    {
                        dic_ComboBox2[ type ].SelectedIndex = 1;
                    }
                }

                // 设置模拟量的报警类型
                foreach ( string type in cfg.KJSysParas2.KJAnalogAlarms_dic.Keys )
                {
                    dic_TypesCheckBox2[ type ].Checked = cfg.KJSysParas2.KJAnalogAlarms_dic[ type ];
                }

                // 设置开关量的报警类型
                foreach ( string type in cfg.KJSysParas2.KJDigitalAlarms_dic.Keys )
                {
                    dic_TypesCheckBox2[ type ].Checked = cfg.KJSysParas2.KJDigitalAlarms_dic[ type ];
                }

                // 设置传感器报警状态
                foreach ( string type in cfg.KJSysParas2.KJAlarmStates_dic.Keys )
                {
                    dic_StatesCheckBox2[ type ].Checked = cfg.KJSysParas2.KJAlarmStates_dic[ type ];
                }
            }
        }

        /// <summary>
        /// 把设置信息保存到配置文件中
        /// </summary>
        internal virtual void ConvertParasTo( )
        {
            // 设置开关量的状态显示
            string[ ] kKeys = cfg.KJSysParas.KJDigitalStates_dic.Keys.ToArray( );
            for ( int i = 0; i < kKeys.Length; i++ )
            {
                string type = kKeys[ i ];
                if ( dic_ComboBox[ type ].SelectedIndex == 0 )
                {
                    // 1 开 0 停
                    cfg.KJSysParas.KJDigitalStates_dic[ type ] = 1;
                }
                else
                {
                    cfg.KJSysParas.KJDigitalStates_dic[ type ] = 0;
                }
            }

            // 设置模拟量的报警类型
            kKeys = cfg.KJSysParas.KJAnalogAlarms_dic.Keys.ToArray( );
            foreach ( string type in kKeys )
            {
                cfg.KJSysParas.KJAnalogAlarms_dic[ type ] = dic_TypesCheckBox[ type ].Checked;
            }

            // 设置开关量的报警类型
            kKeys = cfg.KJSysParas.KJDigitalAlarms_dic.Keys.ToArray( );
            foreach ( string type in kKeys )
            {
                cfg.KJSysParas.KJDigitalAlarms_dic[ type ] = dic_TypesCheckBox[ type ].Checked;
            }

            // 设置传感器报警状态
            kKeys = cfg.KJSysParas.KJAlarmStates_dic.Keys.ToArray( );
            foreach ( string type in kKeys )
            {
                cfg.KJSysParas.KJAlarmStates_dic[ type ] = dic_StatesCheckBox[ type ].Checked;
            }
            if ( !cfg.IsSingleKJSystem )
            {
                foreach ( string type in cfg.KJSysParas2.KJDigitalStates_dic.Keys )
                {
                    if ( dic_ComboBox2[ type ].SelectedIndex == 0 )
                    {
                        cfg.KJSysParas2.KJDigitalStates_dic[ type ] = 0;
                    }
                    else
                    {
                        cfg.KJSysParas2.KJDigitalStates_dic[ type ] = 1;
                    }
                }

                // 设置模拟量的报警类型
                foreach ( string type in cfg.KJSysParas2.KJAnalogAlarms_dic.Keys )
                {
                    cfg.KJSysParas2.KJAnalogAlarms_dic[ type ] = dic_TypesCheckBox2[ type ].Checked;
                }

                // 设置开关量的报警类型
                foreach ( string type in cfg.KJSysParas2.KJDigitalAlarms_dic.Keys )
                {
                    cfg.KJSysParas2.KJDigitalAlarms_dic[ type ] = dic_TypesCheckBox2[ type ].Checked;
                }

                // 设置传感器报警状态
                foreach ( string type in cfg.KJSysParas2.KJAlarmStates_dic.Keys )
                {
                    cfg.KJSysParas2.KJAlarmStates_dic[ type ] = dic_StatesCheckBox2[ type ].Checked;
                }
            }
        }

        internal void SaveParas( )
        {
            if ( MessageBox.Show( this , "您想保存联网参数吗?" , "保存提示" , MessageBoxButtons.YesNo , MessageBoxIcon.Information ) == DialogResult.Yes )
            {
                ConvertParasTo( );

                // mainForm.SaveParas();
                cfg.WriteConfig( );
            }
        }

        /// <summary>
        /// 获取报警选项
        /// </summary>
        //internal void GetAlarmOprions()
        //{
        //    // 设置传感器类型报警选项
        //    if (mainForm.typeOptions.list_Types.Count > 0)
        //    {
        //        foreach (string sTypeName in dic_TypesCheckBox.Keys)
        //        {
        //            dic_TypesCheckBox[sTypeName].Checked = false;
        //        }
        //        foreach (MonitoringUnitType.Types type in mainForm.typeOptions.list_Types)
        //        {
        //            string sTypeName = type.ToString();
        //            dic_TypesCheckBox[sTypeName].Checked = true;
        //        }
        //    }

        //        foreach (string sTypeName in dic_TypesCheckBox.Keys)
        //        {
        //            dic_TypesCheckBox[sTypeName].Checked = false;
        //        }
        //        foreach (MonitoringUnitType.Types type in mainForm.typeOptions.list_Types)
        //        {
        //            string sTypeName = type.ToString();
        //            dic_TypesCheckBox[sTypeName].Checked = true;
        //        }

        //    dic_TypesCheckBox["甲烷"].Checked = true;
        //    //dic_TypesCheckBox["主扇"].Checked = true;

        //    // 设置传感器状态报警选项
        //    if (mainForm.alarmOptions.list.Count > 0)
        //    {
        //        foreach (string stateName in dic_StatesCheckBox.Keys)
        //        {
        //            dic_StatesCheckBox[stateName].Checked = false;
        //        }
        //        foreach (MonitoringUnitState.States state in mainForm.alarmOptions.list)
        //        {
        //            MonitoringUnitState.ChineseStates s = (MonitoringUnitState.ChineseStates)state;
        //            string stateName = s.ToString();
        //            dic_StatesCheckBox[stateName].Checked = true;
        //        }
        //    }
        //    dic_StatesCheckBox["超断电"].Checked = true;
        //}

        /// <summary>
        /// 获取报警选项2
        /// </summary>
        //internal void GetAlarmOprions2()
        //{
        //    // 设置传感器类型报警选项
        //    if (mainForm.typeOptions2.list_Types.Count > 0)
        //    {
        //        foreach (string sTypeName in dic_TypesCheckBox2.Keys)
        //        {
        //            dic_TypesCheckBox2[sTypeName].Checked = false;
        //        }
        //        foreach (MonitoringUnitType.Types type in mainForm.typeOptions2.list_Types)
        //        {
        //            string sTypeName = type.ToString();
        //            dic_TypesCheckBox2[sTypeName].Checked = true;
        //        }
        //    }
        //    dic_TypesCheckBox2["甲烷"].Checked = true;
        //    //dic_TypesCheckBox["主扇"].Checked = true;

        //    // 设置传感器状态报警选项
        //    if (mainForm.alarmOptions2.list.Count > 0)
        //    {
        //        foreach (string stateName in dic_StatesCheckBox2.Keys)
        //        {
        //            dic_StatesCheckBox2[stateName].Checked = false;
        //        }
        //        foreach (MonitoringUnitState.States state in mainForm.alarmOptions2.list)
        //        {
        //            MonitoringUnitState.ChineseStates s = (MonitoringUnitState.ChineseStates)state;
        //            string stateName = s.ToString();
        //            dic_StatesCheckBox2[stateName].Checked = true;
        //        }
        //    }
        //    dic_StatesCheckBox2["超断电"].Checked = true;
        //}

        /// <summary>
        /// 开关量的类型集合的字典
        /// </summary>
        internal Dictionary<string , ComboBox> dic_ComboBox = new Dictionary<string , ComboBox>( );

        internal void SetDigitalState( string type )
        {
            DigitalState state;

            //if (mainForm.dic_DigitalState.ContainsKey(type))
            //{
            //    state = mainForm.dic_DigitalState[type];
            //    if (state.bState0)
            //    {
            //        //this.cbb风机.Text = state.State0;
            //        dic_ComboBox[type].SelectedIndex = 0;
            //    }
            //    else
            //    {
            //        // this.cbb风机.Text = state.State1;
            //        dic_ComboBox[type].SelectedIndex = 1;
            //    }
            //    state.State0 = dic_ComboBox[type].Items[0].ToString();
            //    state.State1 = dic_ComboBox[type].Items[1].ToString();
            //}

            state = SetDigitalStateUI( type , cfg.KJSysParas.KJDigitalStates_dic );
        }

        private DigitalState SetDigitalStateUI( string type , Dictionary<string , int> dic )
        {
            DigitalState state = new DigitalState( type );
            if ( dic.ContainsKey( type ) )
            {
                if ( dic[ type ] == 0 )
                {
                    state.bState0 = true;
                }
                else
                {
                    state.bState0 = false;
                }

                if ( state.bState0 )
                {
                    //this.cbb风机.Text = state.State0;
                    dic_ComboBox[ type ].SelectedIndex = 0;
                }
                else
                {
                    // this.cbb风机.Text = state.State1;
                    dic_ComboBox[ type ].SelectedIndex = 1;
                }
                state.State0 = dic_ComboBox[ type ].Items[ 0 ].ToString( );
                state.State1 = dic_ComboBox[ type ].Items[ 1 ].ToString( );
            }
            return state;
        }

        //internal void GetDigitalState(string type)
        //{
        //    DigitalState state;
        //    if (mainForm.dic_DigitalState.ContainsKey(type))
        //    {
        //        state = mainForm.dic_DigitalState[type];
        //        state.bState0 = dic_ComboBox[type].SelectedIndex == 0 ? true : false;

        //    }
        //}

        internal Dictionary<string , ComboBox> dic_ComboBox2 = new Dictionary<string , ComboBox>( );

        internal void SetDigitalState2( string type )
        {
            DigitalState state;

            //if (mainForm.dic_DigitalState2.ContainsKey(type))
            //{
            //    state = mainForm.dic_DigitalState2[type];
            //    if (state.bState0)
            //    {
            //        //this.cbb风机.Text = state.State0;
            //        dic_ComboBox2[type].SelectedIndex = 0;
            //    }
            //    else
            //    {
            //        // this.cbb风机.Text = state.State1;
            //        dic_ComboBox2[type].SelectedIndex = 1;
            //    }
            //    state.State0 = dic_ComboBox2[type].Items[0].ToString();
            //    state.State1 = dic_ComboBox2[type].Items[1].ToString();
            //}
            state = SetDigitalStateUI( type , cfg.KJSysParas2.KJDigitalStates_dic );
        }

        //internal void GetDigitalState2(string type)
        //{
        //    DigitalState state;
        //    if (mainForm.dic_DigitalState2.ContainsKey(type))
        //    {
        //        state = mainForm.dic_DigitalState2[type];
        //        state.bState0 = dic_ComboBox2[type].SelectedIndex == 0 ? true : false;

        //    }
        //}

        /// <summary>
        /// 设置报警选项
        /// </summary>
        internal void SetAlarmOptions( )
        {
            // 设置传感器状态报警选项
            // 暂停语音报警的更新

            bool bAlarm = mainForm.IsSpeeching;
            mainForm.IsSpeeching = false;
            try
            {
                mainForm.typeOptions.list_Types.Clear( );
                foreach ( string sTypeName in dic_TypesCheckBox.Keys )
                {
                    if ( dic_TypesCheckBox[ sTypeName ].Checked )
                    {
                        mainForm.typeOptions.AddTypeName( sTypeName );
                    }
                }

                // 设置传感器状态报警选项
                mainForm.alarmOptions.list.Clear( );
                foreach ( string stateName in dic_StatesCheckBox.Keys )
                {
                    if ( dic_StatesCheckBox[ stateName ].Checked )
                    {
                        mainForm.alarmOptions.AddStateName( stateName );
                    }
                }
            }
            catch ( Exception ex )
            {
                throw new Exception( "SetAlarmOptions error: " + ex.Message );
            }

            // 恢复语音报警
            mainForm.IsSpeeching = bAlarm;
        }

        /// <summary>
        /// 设置报警选项 2
        /// </summary>
        internal void SetAlarmOptions2( )
        {
            // 设置传感器状态报警选项
            // 暂停语音报警的更新

            bool bAlarm = mainForm.IsSpeeching;
            mainForm.IsSpeeching = false;
            try
            {
                mainForm.typeOptions2.list_Types.Clear( );
                foreach ( string sTypeName in dic_TypesCheckBox2.Keys )
                {
                    if ( dic_TypesCheckBox2[ sTypeName ].Checked )
                    {
                        mainForm.typeOptions2.AddTypeName( sTypeName );
                    }
                }

                // 设置传感器状态报警选项
                mainForm.alarmOptions2.list.Clear( );
                foreach ( string stateName in dic_StatesCheckBox2.Keys )
                {
                    if ( dic_StatesCheckBox2[ stateName ].Checked )
                    {
                        mainForm.alarmOptions2.AddStateName( stateName );
                    }
                }
            }
            catch ( Exception ex )
            {
                throw new Exception( "SetAlarmOptions error: " + ex.Message );
            }

            // 恢复语音报警
            mainForm.IsSpeeching = bAlarm;
        }

        internal void btnSave_Click( object sender , EventArgs e )
        {
            this.Close( );
            SaveParas( );
        }

        internal void btnClose_Click( object sender , EventArgs e )
        {
            this.Close( );
        }

        internal void btnAlarmParas_Click( object sender , EventArgs e )
        {
            this.SetAlarmOptions( );

            //mainForm.SaveAlarmOptions();

            if ( !cfg.IsSingleKJSystem )
            {
                this.SetAlarmOptions2( );

                //  mainForm.SaveAlarmOptions2();
            }
            ConvertParasTo( );

            KWControl.Instance( ).ReSetDigitalStates( );
        }

        internal void btnAuthoring_Click( object sender , EventArgs e )
        {
            // AuthorizationForm form = new AuthorizationForm(mainForm);
            // form.ShowDialog();
        }
    }
}